package org.jetbrains.plugins.scala.codeInsight.hints;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;

import javax.swing.*;
import java.awt.*;
import java.lang.reflect.Method;
import java.util.ResourceBundle;
import java.util.function.Consumer;
import java.util.function.Supplier;

import static org.jetbrains.plugins.scala.codeInsight.ScalaCodeInsightSettings.MAX_PRESENTATION_LENGTH;
import static org.jetbrains.plugins.scala.codeInsight.ScalaCodeInsightSettings.MIN_PRESENTATION_LENGTH;

public final class GeneralSettingsPanel {

    private JCheckBox obviousTypesCheckbox;
    private JCheckBox preserveIndentsCheckBox;
    private JPanel panel;
    private JSpinner presentationLengthSpinner;
    private final SpinnerNumberModel presentationLengthModel;

    private final Supplier<Boolean> obviousTypesLoader;
    private final Supplier<Boolean> preserveIndentsLoader;
    private final Supplier<Integer> presentationLengthLoader;
    private final Consumer<Boolean> obviousTypesSaver;
    private final Consumer<Boolean> preserveIndentsSaver;
    private final Consumer<Integer> presentationLengthSaver;


    // TODO Just use public fields?
    public GeneralSettingsPanel(Supplier<Boolean> obviousTypesLoader, Consumer<Boolean> obviousTypesSaver,
                                Supplier<Boolean> preserveIndentsLoader, Consumer<Boolean> preserveIndentsSaver,
                                Supplier<Integer> presentationLengthLoader, Consumer<Integer> presentationLengthSaver) {
        this.obviousTypesLoader = obviousTypesLoader;
        this.preserveIndentsLoader = preserveIndentsLoader;
        this.presentationLengthLoader = presentationLengthLoader;
        this.obviousTypesSaver = obviousTypesSaver;
        this.preserveIndentsSaver = preserveIndentsSaver;
        this.presentationLengthSaver = presentationLengthSaver;
        presentationLengthModel = new SpinnerNumberModel();
        $$$setupUI$$$();
        presentationLengthModel.setMinimum(MIN_PRESENTATION_LENGTH);
        presentationLengthModel.setMaximum(MAX_PRESENTATION_LENGTH);
        presentationLengthModel.setStepSize(1);
        reset();
    }

    public boolean showObviousTypes() {
        return obviousTypesCheckbox.isSelected();
    }

    public boolean getPreserveIntends() {
        return preserveIndentsCheckBox.isSelected();
    }

    public int getPresentationLength() {
        return presentationLengthModel.getNumber().intValue();
    }

    public boolean isModified() {
        return showObviousTypes() != obviousTypesLoader.get() ||
                getPreserveIntends() != preserveIndentsLoader.get() ||
                getPresentationLength() != presentationLengthLoader.get();
    }

    public void saveSettings() {
        obviousTypesSaver.accept(showObviousTypes());
        preserveIndentsSaver.accept(getPreserveIntends());
        presentationLengthSaver.accept(getPresentationLength());
    }

    public void reset() {
        obviousTypesCheckbox.setSelected(obviousTypesLoader.get());
        preserveIndentsCheckBox.setSelected(preserveIndentsLoader.get());
        presentationLengthModel.setValue(presentationLengthLoader.get());
    }

    private void createUIComponents() {
        presentationLengthSpinner = new JSpinner(presentationLengthModel);
        ((JSpinner.DefaultEditor) presentationLengthSpinner.getEditor()).getTextField().setColumns(3);
    }

    public JPanel getPanel() {
        return panel;
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        panel = new JPanel();
        panel.setLayout(new GridLayoutManager(4, 4, new Insets(0, 0, 0, 0), -1, -1));
        final JLabel label1 = new JLabel();
        this.$$$loadLabelText$$$(label1, this.$$$getMessageFromBundle$$$("messages/ScalaCodeInsightBundle", "inlay.hints.limit.hint.length.to"));
        panel.add(label1, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        panel.add(presentationLengthSpinner, new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JLabel label2 = new JLabel();
        this.$$$loadLabelText$$$(label2, this.$$$getMessageFromBundle$$$("messages/ScalaCodeInsightBundle", "inlay.hints.characters"));
        panel.add(label2, new GridConstraints(2, 2, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final Spacer spacer1 = new Spacer();
        panel.add(spacer1, new GridConstraints(2, 3, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
        final Spacer spacer2 = new Spacer();
        panel.add(spacer2, new GridConstraints(3, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        preserveIndentsCheckBox = new JCheckBox();
        this.$$$loadButtonText$$$(preserveIndentsCheckBox, this.$$$getMessageFromBundle$$$("messages/ScalaCodeInsightBundle", "inlay.hints.respect.vertical.alignment"));
        panel.add(preserveIndentsCheckBox, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        obviousTypesCheckbox = new JCheckBox();
        this.$$$loadButtonText$$$(obviousTypesCheckbox, this.$$$getMessageFromBundle$$$("messages/ScalaCodeInsightBundle", "show.types.even.if.they.are.obvious"));
        panel.add(obviousTypesCheckbox, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        label1.setLabelFor(presentationLengthSpinner);
    }

    private static Method $$$cachedGetBundleMethod$$$ = null;

    private String $$$getMessageFromBundle$$$(String path, String key) {
        ResourceBundle bundle;
        try {
            Class<?> thisClass = this.getClass();
            if ($$$cachedGetBundleMethod$$$ == null) {
                Class<?> dynamicBundleClass = thisClass.getClassLoader().loadClass("com.intellij.DynamicBundle");
                $$$cachedGetBundleMethod$$$ = dynamicBundleClass.getMethod("getBundle", String.class, Class.class);
            }
            bundle = (ResourceBundle) $$$cachedGetBundleMethod$$$.invoke(null, path, thisClass);
        } catch (Exception e) {
            bundle = ResourceBundle.getBundle(path);
        }
        return bundle.getString(key);
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadLabelText$$$(JLabel component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setDisplayedMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadButtonText$$$(AbstractButton component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return panel;
    }

}
